<template>
    <div>
        <Bread></Bread>
        <el-card>
            <!-- 添加角色 -->
            <el-button type="primary" @click="addRoles">添加角色</el-button>

            <!-- 角色列表渲染 -->
            <template>
                <el-table :data="roles" stripe style="width: 100%" border>
                    <el-table-column type="expand" width="80">
                        <template slot-scope="scope">
                            <!-- 权限 -->
                            <el-row v-for="item in scope.row.children" :key="item.id" class="oneRow" type="flex"
                                    align="middle">
                                    <!-- 一级权限 -->
                                    <el-col :span="6" type="flex" align="middle">
                                        <el-tag closable type="primary" @close="delone(scope.row.id, item.id)">
                                            {{ item.authName }}
                                        </el-tag>
                                        <span class="el-icon-caret-right"></span>
                                    </el-col>

                                    <el-col :span="18">
                                        <!-- 二级权限 -->
                                        <el-row v-for="it in item.children" :key="it.id" type="flex" align="middle">
                                            <el-col :span="5">
                                                <el-tag closable type="success" @close="deltwo(scope.row.id, it.id)">
                                                    {{ it.authName }}
                                                </el-tag>
                                                <span class="el-icon-caret-right"></span>
                                            </el-col>
                                            <el-col :span="19">
                                                <el-tag closable type="warning" v-for="i in it.children" :key="i.id"
                                                    @close="delthree(scope.row.id, i.id)">
                                                    {{ i.authName }}
                                                </el-tag>
                                            </el-col>
                                        </el-row>
                                    </el-col>
                                </el-row>

                        </template>
                    </el-table-column>
                    <el-table-column type="index" label="#" width="80">
                    </el-table-column>
                    <el-table-column prop="roleName" label="角色名称">
                    </el-table-column>
                    <el-table-column prop="roleDesc" label="角色描述">
                    </el-table-column>
                    <el-table-column prop="right" label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" type='primary' @click="editRoles(scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="delRoles(scope.row.id)">删除</el-button>
                            <el-button size="mini" type="warning" @click="saveright(scope.row.id)">分配权限</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>


            <!-- 分配权限 -->
            <el-dialog title="分配权限" :visible.sync="saverightmodel" width="70%">
                <span>
                    <el-tree :data="treeList" :props="defaultProps" default-expand-all show-checkbox node-key="id"
                        :default-checked-keys="arr" ref="tree"></el-tree>
                </span>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="saverightmodel = false">取 消</el-button>
                    <el-button type="primary" @click="sureright">确 定</el-button>
                </span>
            </el-dialog>

            <!-- 角色模态框 -->
            <Rdialog :roleDialog="roleDialog" :rolesForm="rolesForm" @getRoles="getRoles"></Rdialog>
        </el-card>
    </div>
</template>

<script>
// 引入模态框
import Rdialog from "../module/Rdialog.vue";
// 引入面包屑
import Bread from "../mbx.vue";
// 引入api
import { rolesList, delroles, delrights, rightList,sureright } from "@/utils/ApI（roles）/rolesAPI";

// 引入element-ui
import { MessageBox } from 'element-ui';

export default {
    components: {
        Rdialog, Bread
    },
    data() {
        return {
            roles: [],

            // 模态框的基本数据
            roleDialog: {
                // 类型
                type: '',
                // 标题
                title: '',
                //添加和编辑 变量控制显示与隐藏
                rolesModel: false
            },
            // 添加
            rolesForm: {
                roleName: "",
                roleDesc: "",
            },
            // 分配权限的模态框
            saverightmodel: false,

            //权限树配置
            defaultProps: {
                children: 'children',
                label: 'authName'
            },
            // 权限树分配的数据
            treeList: [],
            //  存放所有叶子节点
            arr: [],
            rolesid: ""

        };
    },

    mounted() {
        this.getRoles();
        this.getTreeList()
    },

    methods: {
        // 获取角色列表
        async getRoles() {
            let res = await rolesList()
            this.roles = res.data
            console.log(res);
        },

        // 添加角色
        addRoles() {
            this.roleDialog = {
                // 类型
                type: 'add',
                // 标题
                title: '添加角色',
                // 变量控制显示与隐藏
                rolesModel: true
            }
        },

        // 编辑角色
        editRoles(row) {
            this.roleDialog = {
                // 类型
                type: 'edit',
                // 标题
                title: '编辑角色',
                // 变量控制显示与隐藏
                rolesModel: true
            };
            console.log(row);
            this.rolesForm = {
                roleName: row.roleName,
                roleDesc: row.roleDesc,
                id: row.id
            }

        },

        // 删除
        delRoles(id) {
            MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let res = await delroles(id)
                if (res.meta.status == 200) {
                    this.$message({
                        type: 'success',
                        message: res.meta.msg
                    });
                } else {
                    this.$message({
                        type: 'error',
                        message: res.meta.msg
                    });
                }
                this.getRoles()

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        // 删除一级权限
        async delone(rolesid, rightid) {
            //   console.log(rolesid,rightid);
            let res = await delrights(rolesid, rightid)
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg)
            } else {
                this.$message.error(res.meta.msg)
            }
            this.getRoles()
        },

        // 删除二级权限
        async deltwo(rolesid, rightid) {
            //   console.log(rolesid,rightid);
            let res = await delrights(rolesid, rightid)
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg)
            } else {
                this.$message.error(res.meta.msg)
            }
            this.getRoles()
        },


        // 删除三级权限
        async delthree(rolesid, rightid) {
            //   console.log(rolesid,rightid);
            let res = await delrights(rolesid, rightid)
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg)
            } else {
                this.$message.error(res.meta.msg)
            }
            this.getRoles()
        },



        // 分配权限弹出模态框
        saveright(id) {
            this.rolesid = id
            this.saverightmodel = true
            this.arr = []
            var index = this.roles.findIndex(item => {
                return item.id == id
            })
            //    console.log();
            this.getChidren(this.roles[index].children)
        },

        // 获取所有权限列表树
        async getTreeList() {
            let res = await rightList()
            this.treeList = res.data
        },
        getChidren(val) {
            val.forEach(item => {
                if (item.children) {
                    return this.getChidren(item.children)
                } else {
                    this.arr.push(item.id)
                }
            })
        },
        // 角色授权
        async sureright() {
            var checkedAll = this.$refs.tree.getCheckedKeys()
            var halfChecked = this.$refs.tree.getHalfCheckedKeys()
            var rids = [
                ...checkedAll, ...halfChecked
            ].join(',')
            // console.log(rids);
            let res = await sureright(this.rolesid, {rids})
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg)
            } else {
                this.$message.error(res.meta.msg)
            }
            this.saverightmodel = false
            this.getRoles()
        }
    },
};
</script>

<style lang="scss" scoped>
.el-tag{
    margin-top: 5px;
    margin-left:5px ;
}
</style>